package com.zryc.shop.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.pms.entity.Product;
import com.zryc.shop.pms.entity.ProductAttribute;
import com.zryc.shop.pms.entity.ProductCategory;
import com.zryc.shop.pms.mapper.ProductCategoryMapper;
import com.zryc.shop.pms.service.IProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zryc.shop.ums.entity.Admin;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    /**
     * 分页查询全部商品分类列表
     *
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<ProductCategory> list(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<ProductCategory> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        return page(page, wrapper);
    }

    /**
     * 构建商品列表树
     *
     * @param productCategories
     * @return
     */
    @Override
    public Object buildTree(List<ProductCategory> productCategories) {
        List<ProductCategory> rootProductCategoryList = new ArrayList<>();
        for (ProductCategory productCategory : productCategories) {
            if (productCategory.getParentId() == 0) {
                rootProductCategoryList.add(productCategory);
            }
        }
        for (ProductCategory productCategory : rootProductCategoryList) {
            childrenProductCategory(productCategories, productCategory);
        }
        return rootProductCategoryList;
    }

    /**
     * 修改是否显示在导航栏
     *
     * @param ids
     * @param navStatus
     * @return
     */
    @Override
    public Object updateNavStatus(String ids, Integer navStatus) {
        String[] idList = ids.split(",");
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", idList);
        updateWrapper.set("nav_status", navStatus);
        update(updateWrapper);
        return null;
    }

    /**
     * 修改是否显示
     *
     * @param ids
     * @param showStatus
     * @return
     */
    @Override
    public Object updateShowStatus(String ids, Integer showStatus) {
        String[] idList = ids.split(",");
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", idList);
        updateWrapper.set("show_status", showStatus);
        update(updateWrapper);
        return null;
    }

    /**
     * 修改商品类别
     *
     * @param id
     * @param productCategory
     * @return
     */
    @Override
    public Object updateCategory(Long id, ProductCategory productCategory) {
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        baseMapper.update(productCategory, updateWrapper);
        return null;
    }

    /**
     * 删除商品类别
     *
     * @param id
     * @return
     */
    @Override
    public Object removeCategory(Long id) {
        baseMapper.deleteById(id);//删除类别表数据
        baseMapper.deleteFromMid(id);//删除中间表数据
        return null;
    }

    private ProductCategory childrenProductCategory(List<ProductCategory> productCategoryList, ProductCategory productCategory) {
        List<ProductCategory> children = new ArrayList<>();
        for (ProductCategory m : productCategoryList) {
            if (m.getParentId().equals(productCategory.getId())) {
                children.add(childrenProductCategory(productCategoryList, m));
            }
        }
        productCategory.setChildren(children);
        return productCategory;
    }
}
